package com.yizhiliulianta.auth.common.config;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import com.yizhiliulianta.auth.common.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * mvc全局处理
 */
@Configuration
public class GlobalConfig extends WebMvcConfigurationSupport {
    @Override
    //请求和响应之间进行数据转换
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        converters.add(mappingJackson2HttpMessageConverter());
    }
    //消息转换器
    private MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){
        //json转java对象（反序列化）
        ObjectMapper objectMapper = new ObjectMapper();
        //设置json返回如果是空对象，不会报错
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS,false);
        //json属性为null，不进行序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        //实现json的消息转换器
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(objectMapper);
        return converter;
    }
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**");
    }

}
